解决Failed to connect to github.com port 443 after 21058 ms Couldn't connect to server
解决Failed to connect to github.com port 443 after 21058 ms Couldn't connect to server
Frank leeGit: 解决 Failed to connect to github.com port 443 after 21058 ms Couldn’t connect to server
1. 问题背景
在使用 IDEA 通过 Git 将代码 push 到 GitHub 时,遇到了以下错误:
1 | Push failed unable to access 'https://github.com/xxxxx/xxxxx.git/': |
2. 出错原因
经过查阅资料后,发现这是由于在使用 Git 时启用了网络代理,导致 Git 改变了默认端口,从而无法连接到 GitHub。因此,我们需要手动配置 Git 的代理端口来解决该问题。
3. 解决方案
3.1 获取代理地址
首先,打开 Windows 设置,点击 网络和 Internet,然后选择 代理。在 手动代理设置 栏目下,点击 编辑 来获取你当前的代理地址。
如果你使用的是 VPN 或代理工具(例如 Clash、V2ray 等),可以在它们的界面中查找代理端口信息。典型的本地代理地址通常为 127.0.0.1,端口号如 7890、1080 等。
3.2 配置 Git 代理
打开命令行(如 CMD 或 Git Bash),输入以下命令为 Git 配置 HTTP 和 HTTPS 的代理:
1 | git config --global http.proxy http://127.0.0.1:7890 |
请将 127.0.0.1:7890 替换为你实际的代理地址和端口。
3.3 重新提交代码
完成代理设置后,再次尝试提交代码:
1 | git push origin branch_name |
此时问题应该已经解决,代码可以成功推送到 GitHub。
4. 其他注意事项
4.1 检查代理是否生效
可以通过以下命令来检查当前的 Git 代理设置是否正确配置:
1 | git config --global --get http.proxy |
如果返回的代理地址和端口与配置一致,说明代理设置已生效。
4.2 取消代理设置
如果你日后不再使用代理,或者想切换到其他网络环境中,可以通过以下命令取消代理配置:
1 | git config --global --unset http.proxy |
4.3 降低 Git 超时的可能性
如果推送的数据量很大,可能会导致连接超时,你可以尝试降低 Git 的 buffer 大小,以避免此类问题:
1 | git config --global http.postBuffer 52428800 |
4.4 使用 SSH 而不是 HTTPS
如果你经常遇到网络问题,或者不希望配置代理,也可以考虑使用 SSH 来代替 HTTPS 推送代码:
- 生成 SSH 密钥:
1
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
- 将公钥添加到 GitHub:
打开~/.ssh/id_rsa.pub,复制其中的内容并将其粘贴到 GitHub 账户的 SSH keys 页面。 - 修改 Git 的远程 URL 为 SSH:
1
git remote set-url origin git@github.com:username/repository.git
- 重新推送代码:
1
git push origin branch_name



